package com.yhuso.test.serializableAndExternalizable;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * Created by ehsy_it on 2016/4/1.
 */
public class SerializableDemo1 {
    public static void main(String[] args) {
        User1 user1 = new User1("yhuso", 25);
        System.out.println(user1.toString());
        System.out.println("---");

        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("tempFile"));
            objectOutputStream.writeObject(user1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(objectOutputStream);
        }

        ObjectInputStream ois = null;
        File file = new File("tempFile");
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            User1 newUser = (User1)ois.readObject();
            System.out.println(newUser.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
